<template>
  <!-- 添加或修改报工列对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="12" style="margin-top: 30px;">
              <el-form-item label="工单剩余数量" prop="count">
                <el-input-number v-model="form.remainCount" disabled placeholder="请输入工单剩余数量"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" style="margin-top: 30px;">
              <el-form-item label="报工数量" prop="count">
               <el-input-number v-model="form.count" @change="baogongCount" placeholder="请输入报工数量"/>
                <!-- <el-input-number v-model="form.count"   placeholder="请输入报工数量"/> -->
              </el-form-item>
            </el-col>
            <el-col :span="12" style="margin-top: 30px;">
              <el-form-item label="工时（小时）" prop="hour">
                <el-input v-model="form.hour" type="number" placeholder="请输入工时"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" style="margin-top: 30px;">
              <el-form-item label="单价" prop="money">
                <el-input v-model="form.money" placeholder="请输入单价" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="12" style="margin-top: 30px;">
              <el-form-item label="总价" prop="allMoney">
                <el-input v-model="form.allMoney" placeholder="请输入总价" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="8" style="margin-top: 30px;">
             <el-form-item label="报工日期:" prop="doTime">
              <el-date-picker clearable
                              v-model="form.doTime"
                              :default-value="currentDateTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择报工日期">
              </el-date-picker>
            </el-form-item>
            </el-col>
            <el-col :span="24"style="margin-top: 30px;" >
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { addAction, getById, updateAction } from '@/api/manage';
  import moment from 'moment';
  import {parseTime} from "@/utils/common";
  export default {
    name: "BaogongFormList",
    data() {
      return {
        // 遮罩层
        loading: true,
        activeName: "first",
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          count: [
            { required: true, message: '报工数量不能为空', trigger: 'blur' },
            { validator: this.validateCount, trigger: 'blur' }
          ],
        },
        url: {
          getById:'/goodsSaledoGongxuUserMoney/goodsSaledoGongxuUserMoney/',
          addBaogong:'/goodsSaledoGongxuUserMoney/goodsSaledoGongxuUserMoney/addBaogong',
          edit:'/goodsSaledoGongxuUserMoney/goodsSaledoGongxuUserMoney/edit',
        },
      };
    },
    created() {

    },
    computed: {
    currentDateTime() {
       this.form.doTime = parseTime(new Date())
      return new Date();
    }
  },
    methods: {
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          xuhao: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          doTime: null,
          userId: null,
          userName: null,
          money: null,
          count: '',
          hour: null,
          allMoney: null,
          parentId: null,
          successCount: null,
          errorCount: null,
          successLv: null,
          status: "0",
          zhijianDate: null,
          zhijianUser: null,
          zhijianId: null,
          type: "0"
        };
        this.resetForm("form");
      },
      /** 新增按钮操作 */
      baogongShow(row) {
        this.reset();
        this.form.money=row.money;
        this.form.parentId=row.id;
        this.form.remainCount = row.remainCount;
        this.form.count = row.remainCount
        this.open = true;
        this.title = "添加1报工";
      },
      validateCount(rule, value, callback) {
        if (value <= 0) {
          callback(new Error('报工数量不能为0'));
        } /*else if (value > this.form.remainCount) {
          callback(new Error('报工数量不能超过工单剩余数量'));
        } */else {
          callback();
        }
      },
      baogongCount() {
        let that = this;
        if (that.form.money && that.form.count) {
          if (that.form.count > that.form.remainCount) {
            that.$message.error('报工数量不能超过工单剩余数量');
            that.form.count = that.form.remainCount;
          } else {
            let totlePrice = this.functionNumberMul(that.form.count, that.form.money);
            that.$set(that.form, "allMoney", totlePrice);
          }
        }
      },
      functionNumberMul(arg1, arg2) {
        var m = 0;
        var s1 = arg1.toString();
        var s2 = arg2.toString();
        try {
          m += s1.split(".")[1].length;
        } catch (e) {
        }
        try {
          m += s2.split(".")[1].length;
        } catch (e) {
        }
        return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
        getById(this.url.getById, id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改报工";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateAction(this.url.edit, this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.$emit('ok');
              });
            } else {
              addAction(this.url.addBaogong, this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.$emit('ok');
              });
            }
          }
        });
      },
    }
  };
</script>
